package com.example.springboot_demo.controller;

import com.alibaba.fastjson2.JSON;
import com.example.springboot_demo.pojo.User;
import com.example.springboot_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@RestController
public class UserController {
    @Autowired
    UserService userService;


    //检查用户名是否重复
    @RequestMapping(value = "/checkUM", method = RequestMethod.POST)
    public String checkUM(@RequestBody User user) {
        return userService.checkUM(user);
    }


    //注册用户
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestBody Map<String, User> map) {
        return userService.register(map.get("user"));
    }


    //检查登录
    @RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
    public String checkLogin(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) {
        User user = JSON.parseObject(JSON.toJSONString(map.get("user")), User.class);
        String flag = map.get("flag").toString();
        return userService.checkLogin(user, Boolean.valueOf(flag), request, response);
    }

    //更新用户密码根据用户名
    @RequestMapping(value = "/updateByName",method = RequestMethod.POST)
    public String updatePasswordByName(@RequestBody Map<String,String>map,HttpServletRequest request,HttpServletResponse response){
        String userName=map.get("userName");
        String oldPwd=map.get("oldPwd");
        String newPwd1=map.get("newPwd1");
        String newPwd2=map.get("newPwd2");
        return userService.updatePassword(userName,oldPwd,newPwd1,newPwd2,request,response);
    }


    //登出
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        return "ok";
    }


}
